package com.ls.task.listener;

import java.util.Iterator;
import java.util.Map;

import javax.servlet.ServletContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.web.context.ServletContextAware;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.ls.fw.commons.core.utils.collections.MapUtils;
import com.ls.fw.commons.core.utils.collections.MapUtils.KeyValueMapper;
import com.ls.fw.data.annotation.FwRequiresPermissions;

public class SpringmvcListener implements ServletContextAware, ApplicationListener<ContextRefreshedEvent>{
	
	protected Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		final ApplicationContext wc = event.getApplicationContext();  
	}


	
	private void controller(Map<String,Object> map) {
		System.out.println(map);
		try {
			MapUtils.traversal(map, new KeyValueMapper<String,Object>(){

				@Override
				public boolean next(String arg0, Object arg1, int arg2) throws Exception {
					 
					return true;
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private void mapping(ApplicationContext wc) {
		RequestMappingHandlerMapping rmhp = wc.getBean(RequestMappingHandlerMapping.class);  
        Map<RequestMappingInfo, HandlerMethod> map = rmhp.getHandlerMethods();  
        for (Iterator<RequestMappingInfo> iterator = map.keySet().iterator(); iterator    
                .hasNext();) {    
            RequestMappingInfo info = iterator.next();  
//            System.out.print(info.getConsumesCondition());  
//            System.out.print(info.getCustomCondition());  
//            System.out.print(info.getHeadersCondition());  
//            System.out.print(info.getMethodsCondition());  
//            System.out.print(info.getParamsCondition());  
//            System.out.print(info.getPatternsCondition());  
//            System.out.print(info.getProducesCondition());  
//  
//            System.out.print("===");  
            
            HandlerMethod method = map.get(info);  
            
//            System.out.print(method.getMethodAnnotation(RequestMapping.class).params()[0]);  
            System.out.println();  
            FwRequiresPermissions fw =  method.getBeanType().getAnnotation(FwRequiresPermissions.class);
            if(fw != null){
            	
            }
            System.out.println(method.getBeanType().getName()+"."+method.getMethod().getName()+":"+info.getPatternsCondition());  
        }
	}



	@Override
	public void setServletContext(ServletContext servletContext) {
		// TODO Auto-generated method stub
	}
	
}